<#assign className=table.className>
<#assign classNameLower=className?uncap_first>
<#assign hasColumn="com.auto.ftl.FtlHasColumnMethod"?new()>
package ${basepackage}.service.impl.${bizPackage};

import ${basepackage}.entity.${bizPackage}.${className}Entity;

import ${basepackage}.mapper.${bizPackage}.${className}Mapper;
import ${basepackage}.service.${bizPackage}.${className}Service;
import ${basepackage}.common.utils.PreconditionsUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.cjy.common.message.BaseResult;
import ${basepackage}.vo.${bizPackage}.${className}Vo;


/**
 * ${bizName}${table.sqlName}的Service实现
 *
 * @author ${authorName}
 * @version 1.0
 * @Date ${now?string('yyyy年MM月dd日 HH:mm:ss')}
 */
@Service
public class ${className}ServiceImpl extends ServiceImpl<${className}Mapper, ${className}Entity> implements  ${className}Service{

    private Logger logger=LoggerFactory.getLogger(getClass());

    @Autowired
    private ${className}Mapper ${classNameLower}Mapper;

        /**
         * 获取详情
         * @param id 主键id
         * @return
         */
        @Override
        public ${className}Entity getByIdDetails(Long id) {
                return this.getById(id);
        }



/**
 * 分页查询
 * @param req 查询参数
 * @return
 */
        @Override
        public List<${className}Entity> pageList(${className}Vo req) {
        BaseResult.startPage(req);
            return ${classNameLower}Mapper.queryList(req);
        }

    /**
     * 批量逻辑删除
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteLogicBatchById(Set<Long> ids){

            ${className}Entity entity = new ${className}Entity();
            this.baseMapper.update(entity,
            new QueryWrapper<${className}Entity>()
            .in("id",ids)
            );

    }

    /**
     * 保存或修改接口
     *
     * @param pageObj 实体对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ${className}Entity saveOrUpdateDo(${className}Entity pageObj){
            PreconditionsUtils.checkNotNull(pageObj,"保存对象不能为空");

            if(logger.isDebugEnabled()){
            logger.debug("保存"+pageObj);
            }

                //验证字段的唯一性
    //  PreconditionsUtils.checkArgument(iSexist(pageObj.getId(),"name",pageObj.getName()),"名称["+pageObj.getName()+"]已经存在");


        //设置更新时间
        Date now=new Date();
<#--根据表的字段需要生成不同的方法-->
<#list table.columns as column>
<#if column.columnNameLower=='updator'>
        pageObj.setUpdator(loginUser.getUserId());
<#elseif column.columnNameLower=='updatorName'>
        pageObj.setUpdatorName(UserHandler.getLoginUserName());
<#elseif column.columnNameLower=='updateTime'>
        pageObj.setUpdateTime(now);
</#if>
</#list>
        //新增记录
        if(pageObj.getId()==null){
        //设置默认属性
<#--根据表的字段需要生成不同的方法-->
<#list table.columns as column>
<#if column.columnNameLower=='removed'>
        pageObj.setRemoved(false);
<#elseif column.columnNameLower=='creator'>
        pageObj.setCreator(loginUser.getUserId());
<#elseif column.columnNameLower=='creatorName'>
        pageObj.setCreatorName(UserHandler.getLoginUserName());
<#elseif column.columnNameLower=='createTime'>
        pageObj.setCreateTime(now);
</#if>
</#list>
        //新增
        this.save(pageObj);
        return pageObj;
        }

        //更新记录
        ${className}Entity dbObj=this.getById(pageObj.getId());
        PreconditionsUtils.checkNotNull(dbObj,"更新的记录不存在");
        //不能修改的字段
<#--根据表的字段需要生成不同的方法-->
<#list table.columns as column>
<#if column.columnNameLower=='removed'>
        pageObj.setRemoved(dbObj.getRemoved());
<#elseif column.columnNameLower=='creator'>
        pageObj.setCreator(dbObj.getCreator());
<#elseif column.columnNameLower=='creatorName'>
        pageObj.setCreatorName(dbObj.getCreatorName());
<#elseif column.columnNameLower=='createTime'>
        pageObj.setCreateTime(dbObj.getCreateTime());
</#if>
</#list>
        //更新
        this.updateById(pageObj);

        return pageObj;
        }



   


        public boolean iSexist(Long id,String rowName,String rowValue){
        List<String> rowList=new ArrayList<>();
        rowList.add("name");
        rowList.add("username");
        PreconditionsUtils.checkArgument(!rowList.contains(rowName),"此字段不需要判断是否存在");
<#if hasColumn(table.columns,"removed")>
        List<${className}Entity> list=this.baseMapper.selectList(
                new QueryWrapper<${className}Entity>().eq("removed", false).eq(rowName, rowValue).last("limit 1"));
<#else>
        List<${className}Entity> list=this.baseMapper.selectList(
                new QueryWrapper<${className}Entity>().eq("removed", false).eq(rowName, rowValue).last("limit 1"));
</#if>
        if (CollUtil.isEmpty(list)) {
        return false;
        }
        ${className}Entity dbObj=list.get(0);
        //编辑时，此字段跟数据库记录一致，不认为重复
        if(dbObj.getId().equals(id)){
        return false;
        }
        return true;

        }



    /**
     * 通过多个id查询列表
     * @param ids 多个主键id
     * @return
     */
        @Override
        public List<${className}Entity> getListByIds(Set<Long> ids){
        return (List<${className}Entity>) this.listByIds(ids);
        }

  }
